
//在贫血模型中，功能的主要实现位置。

public class Service {
    private StringBuffer checker = new StringBuffer();//交易流水
    private Reponsitory re = new Reponsitory();

    public Service() {

    }

    private Wallet trans(Entity entity) {
        return new Wallet(entity.getId());
    }

    public Wallet getWallet(int id) {
        Entity entity = re.getWalletEntity(id);
        Wallet wallet = trans(entity);
        return wallet;
    }

    //查询余额
    public double getMoney(int id) {
        return re.getMoney(id);
    }

    //充值
    public void deposit(int id, double n) {
        Entity entity = re.getWalletEntity(id);
        Wallet wallet = trans(entity);
        wallet.deposit(n);
        re.updateMoney(1, wallet.getMoney());
        checker.append("充值" + n + "元");
    }

    //提现
    public void CashOut(int id, double n) {
        Entity entity = re.getWalletEntity(id);
        Wallet wallet = trans(entity);
        wallet.CashOut(n);
        re.updateMoney(1, wallet.getMoney());
        checker.append("提现" + n + "元");
    }

    //支付，认为是和提现相同，都是把钱包中的余额向其他账户发送
    public void pay(int id, double n) {
        Entity entity = re.getWalletEntity(id);
        Wallet wallet = trans(entity);
        wallet.CashOut(n);
        re.updateMoney(1, wallet.getMoney());
        checker.append("支付" + n + "元\n");
    }

    //冻结，n为冻结的数目
    public void freeze(int id, double n) {
        Entity entity = re.getWalletEntity(id);
        Wallet wallet = trans(entity);
        //有必要创建吗？
        re.updateFrozenMoney(id, n);
        checker.append("冻结" + n + "元\n");
    }

    //申请透支权限
    public void apply(int id, boolean e) {
        Entity entity = re.getWalletEntity(id);
        Wallet wallet = trans(entity);
        re.updatePermission(id, e);
        checker.append("目前透支准许:" + e+"\n");
    }

    //获取透支额度
    public void getApply(int id, double n) {
        Entity entity = re.getWalletEntity(id);
        Wallet wallet = trans(entity);
        if (wallet.getAllowed())
            System.out.println("申请成功！");
        else {
            System.out.println("申请失败！");
            return;
        }
        checker.append("目前透支额度:" + n+"\n");
    }


    public StringBuffer getChecker() {
        return checker;
    }


}
